package com.xana.acg.mikomiko.frags.live;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.xana.acg.com.app.Fragment;
import com.xana.acg.com.data.DataSource;
import com.xana.acg.com.view.RoundImageView;
import com.xana.acg.com.view.recycler.RecyclerV;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.fac.model.biliLive.TabItem;
import com.xana.acg.fac.model.biliLive.TagItem;
import com.xana.acg.fac.model.biliLive.TagsResp;
import com.xana.acg.fac.net.NetCallBack;
import com.xana.acg.fac.net.Network;
import com.xana.acg.mikomiko.App;
import com.xana.acg.mikomiko.R;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LiveAreaFragment extends Fragment implements DataSource.Callback<TagsResp> {

    private List<String> mTitles;

    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager2)
    ViewPager2 viewPager2;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_live_area;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        Network.biliAPI().getTags().enqueue(new NetCallBack(this));
    }


    private ViewPager2Adapter mAdapter;

    @Override
    public void ok(TagsResp resp) {
//        Log.e("TagsResp", ""+resp);
        mTitles = new ArrayList<>();
        viewPager2.setAdapter(mAdapter = new ViewPager2Adapter());
        for (TabItem item : resp.data.getData()) {
            mTitles.add(item.getName());
            mAdapter.add(new Adapter(item.getList(), new OnItemClick((OnItemClick.Listener) getActivity())));
        }
        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int i) {
                tab.setText(mTitles.get(i));
            }
        }).attach();

    }

    public static class OnItemClick implements RecyclerAdapter.AdapterListener<TagItem>{

        public interface Listener{
            void click(RecyclerAdapter.ViewHolder vh, TagItem item);
        }
        private Listener mListener;

        public OnItemClick(Listener l){
            mListener = l;
        }


        @Override
        public void onItemClick(RecyclerAdapter.ViewHolder holder, TagItem tagItem) {
            mListener.click(holder, tagItem);
        }

        @Override
        public void onItemLongClick(RecyclerAdapter.ViewHolder holder, TagItem tagItem) {

        }
    }

    @Override
    public void fail(String msg) {
        App.showToast(msg);
    }
    static class Adapter extends RecyclerAdapter<TagItem>{
        public Adapter(List<TagItem> items, AdapterListener l){
            super(items, l);
        }
        @Override
        protected int getItemViewType(int position, TagItem tagItem) {
            return R.layout.item_drawer_live_tag;
        }
        @Override
        protected RecyclerAdapter.ViewHolder<TagItem> onCreateViewHolder(View view, int viewType) {
            return new ViewHolder(view);
        }

        static class ViewHolder extends RecyclerAdapter.ViewHolder<TagItem>{
            @BindView(R.id.tagImageView)
            RoundImageView tagImage;
            @BindView(R.id.tagTextView)
            TextView tagText;

            public ViewHolder(View itemView) {
                super(itemView);
            }

            @Override
            protected void onBind(TagItem item) {
                tagImage.setSrc(item.getPic());
                tagText.setText(item.getName());
            }
        }
    }

    static class ViewPager2Adapter extends RecyclerAdapter<Adapter>{

        @Override
        protected int getItemViewType(int position, Adapter s) {
            return R.layout.fragment_recycler;
        }
        @Override
        protected RecyclerAdapter.ViewHolder<Adapter> onCreateViewHolder(View view, int viewType) {
            return new ViewHolder(view);
        }

        static class ViewHolder extends RecyclerAdapter.ViewHolder<Adapter>{
            @BindView(R.id.recycler)
            RecyclerV recycler;
            public ViewHolder(View itemView) {
                super(itemView);
            }
            @Override
            protected void onBind(Adapter adapter) {
                recycler.setLayoutManager(new GridLayoutManager(App.getSingle(), 3));
                recycler.setAdapter(adapter);
            }
        }
    }
}

